package zkdemo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;
import org.junit.Before;
import org.junit.Test;

public class Curator_DistAtomicInt {
	CuratorFramework client;
	
	@Before
	public void init() {
		client = CuratorFrameworkFactory.builder()
				.connectString("192.168.10.188:2181")
				.sessionTimeoutMs(5000)
				.connectionTimeoutMs(3000)
				.retryPolicy(new ExponentialBackoffRetry(1000, 3))
				.namespace("zk-book")
				.build();
		client.start();
	}
	
	@Test
	public void test() throws Exception {
		DistributedAtomicInteger atomicInt = new DistributedAtomicInteger(client, "/atomicInt", new RetryNTimes(3, 1000));
		AtomicValue<Integer> rc = atomicInt.add(1);
		System.out.println("rc="+rc.succeeded()+" pre="+rc.preValue()+" post="+rc.postValue());
	}
}
